package com.tim.file.controller;

import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.tim.controller.BaseContoller;
import com.tim.file.service.SqlLiteServer;
import com.utils.IdWorker;
import com.utils.VerifyUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tim
 * @version 1.0
 * @date 2022/12/19 15:12
 */
@RestController
@RequestMapping("/sqlLite")
public class SqlLiteController extends BaseContoller {
    @Autowired
    Statement sqlLiteSql;

    @Autowired
    SqlLiteServer sqlLiteServer;

    @PostMapping("/insert")
    @ApiOperation(value = "插入sql脚本")
    @DynamicParameters(name = "param",properties = {
            @DynamicParameter(name = "groups",value = "分组",required = true),
            @DynamicParameter(name = "codes",value = "编号",required = true),
            @DynamicParameter(name = "sqlContent",value = "脚本",required = true),
            @DynamicParameter(name = "des",value = "描述",required = true),
            @DynamicParameter(name = "tal",value = "作者",required = true),
            @DynamicParameter(name = "paramKeys",value = "脚本动态参数多个参数,隔开"),
    })
    public Object insert(@RequestBody Map<String, Object> param) throws Exception {
        getParamMapKeyNess(param, "groups", "codes", "sqlContent", "des", "tal");
        String sql = "INSERT INTO `sys_t_sql` ( `ID`, `GROUPS`, `CODES`, `SQL_CONTENT`, `PARAM_KEYS`, `DES`, `TAL`, `C_TIME`, `C_CREATOR` ) " +
                "VALUES('${id}','${groups}','${codes}','${sqlContent}','${param}','${des}','${tal}','${cTime}','sqlLite');";
        String selectSql = "select * from sys_t_sql where 1=1 and groups='${groups}' and codes='${codes}'";
        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("id", IdWorker.getInstant().nextId() + "");
        valueMap.put("groups", param.get("groups") + "");
        valueMap.put("codes", param.get("codes") + "");
        valueMap.put("sqlContent", param.get("sqlContent") + "");
        valueMap.put("paramKeys", param.get("paramKeys") + "");
        valueMap.put("des", param.get("des") + "");
        valueMap.put("tal", param.get("tal") + "");
        valueMap.put("cTime", VerifyUtil.getDate() + "");
        StringSubstitutor sub = new StringSubstitutor(valueMap);
        String selectSqlReplace = sub.replace(selectSql);
        ResultSet resultSet = sqlLiteSql.executeQuery(selectSqlReplace);
        int i = 0;
        while (resultSet.next()) {
            i++;
            break;
        }
        if (i > 0) {
            return doOutError("groups+codes 名字的组合已存在该数据，请换一个组合");
        }
        String replace = sub.replace(sql);
        sqlLiteSql.execute(replace);
        return doOutMsg("插入成功");
    }


    @PostMapping("/del")
    @ApiOperation(value = "删除脚本名")
    @DynamicParameters(name = "param",properties = {
            @DynamicParameter(name = "groups",value = "分组",required = true),
            @DynamicParameter(name = "codes",value = "编号",required = true),
    })
    public Object del(@RequestBody Map<String, Object> param) throws Exception {
        getParamMapKeyNess(param, "groups", "codes");
        String sql = "DELETE from sys_t_sql WHERE 1=1 and groups = '${groups}' and codes = '${codes}'";
        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("groups", param.get("groups") + "");
        valueMap.put("codes", param.get("codes") + "");
        StringSubstitutor sub = new StringSubstitutor(valueMap);
        String replace = sub.replace(sql);
        sqlLiteSql.execute(replace);
        return doOutMsg("删除成功");
    }

    @PostMapping("/list")
    @ApiOperation(value = "查看所有脚本")
    public Object list(@RequestBody(required = false) Map<String, Object> param) throws Exception {
        getParamMapKeyNess(param);
        List<Map<String, Object>> allList = sqlLiteServer.getAllList();
        return doOutData(allList);
    }
}
